<?php
/**
 * xml解析
 *
 * @return unknown
 */
function insert_appmenu(){
	global $G_PGLOB;
	$menu_file = sprintf(G_APPLICATION_BASEPATH.'quanliadmin/docroot/home/user/%s/%s.menu',$G_PGLOB->currentUserID,$G_PGLOB->currentUserID);
	/*if (file_exists($menu_file))
		return file_get_contents($menu_file);*/

    $nav_name = $G_PGLOB->get_cookie_info('nav_name');

	$xml = new DOMDocument();
	$xml->load(G_APPLICATION_BASEPATH.'quanliadmin/docroot/templates/tpls/comm/menu_'.$nav_name.'.xml');
	$menus_1 = $xml->getElementsByTagName('menu1');
	$len_1 = $menus_1->length;
	for ($i=0; $i < $len_1; $i++){
		$menu_item_1 = $menus_1->item($i);
		$permission_id = $menu_item_1->getAttribute('permission_id');
		if ($permission_id != '' && !$G_PGLOB->has_permission($permission_id))
			continue;

		$menu_1_a = array(
          'caption'=>$menu_item_1->getAttribute('caption'),
          'onclick'=>$menu_item_1->getAttribute('onclick'),
          'permission_id'=>$permission_id,
          'pic'=>$menu_item_1->getAttribute('pic'),
        );
	
		$menus_2 = $menu_item_1->getElementsByTagName('menu2');
		$len_2 = $menus_2->length;
		for ($k=0; $k < $len_2; $k++){
			$menu_item_2 = $menus_2->item($k);
			$permission_id = $menu_item_2->getAttribute('permission_id');
			if ($permission_id != '' && !$G_PGLOB->has_permission($permission_id))
				continue;

			$menu_2_a = array(
              'open' => $permission_id >= 100000 && $permission_id <= 110000 ? 1 : 0,
              'caption'=>$menu_item_2->getAttribute('caption'),
              'onclick'=>$menu_item_2->getAttribute('onclick'),
              'permission_id'=>$permission_id
            );

			$menus_3 = $menu_item_2->getElementsByTagName('menu3');
			$len_3 = $menus_3->length;
			if ($len_3 > 0){
				for ($j=0; $j < $len_3; $j++){
					$menu_item_3 = $menus_3->item($j);
					$permission_id = $menu_item_3->getAttribute('permission_id');
					if ($permission_id != '' && !$G_PGLOB->has_permission($permission_id))
						continue;
					$menu_2_a['sub_menus'][] = array(
                        'open' => $permission_id >= 50000 && $permission_id <= 60000 ? 1 : 0,
                        'caption'=>$menu_item_3->getAttribute('caption'),
					    'onclick'=>$menu_item_3->getAttribute('onclick'),
					    'permission_id'=>$permission_id
                    );
				}
			}
			if ($menu_2_a['onclick'] != '' || isset($menu_2_a['sub_menus']))
				$menu_1_a['sub_menus'][] = $menu_2_a;
			
		}
		if ($menu_1_a['onclick'] != '' || isset($menu_1_a['sub_menus']))
			$menu_data[] = $menu_1_a;
	}
	
	//获取定制菜单
	$memoryCache = new memoryCache();
	$menu_array = $memoryCache->get($G_PGLOB->currentUserID);
	if ($menu_array){
		$G_PGLOB->smarty->assign_by_ref('custom_menu',unserialize($menu_array));
	}

	$G_PGLOB->smarty->assign_by_ref('menu_tree',$menu_data);
	$G_PGLOB->smarty->assign('currentUserID',$G_PGLOB->currentUserID);
	$G_PGLOB->smarty->assign('currentUserName',$G_PGLOB->currentUserName);
	
	$menu_content = $G_PGLOB->smarty->fetch('comm/menu_def.tpl');
	$menu_dir = sprintf(G_APPLICATION_BASEPATH.'quanliadmin/docroot/home/user/%s',$G_PGLOB->currentUserID);
	if (!file_exists($menu_dir))
		mkdir($menu_dir,0777,true);
	file_put_contents($menu_file,$menu_content);
	return $menu_content;
}
?>